var checkerUtil = {
	/**
	* get all content images of current web page
	*/
	getImages : function(){
		var docs = this.__getDocs__(window.top.getBrowser().selectedBrowser.contentWindow);
		var images = [];
		for (var index in docs ){
			var doc = docs[index];
			for (var i in doc.getElementsByTagName("img")) {
	            if (doc.images[i].src)	images[doc.images[i].src] = { width: doc.images[i].width, height: doc.images[i].height };
        	}
		}
		return images;
	},
	
	/**
	* get all css images of current web page
	*/
	getCSSImages: function() {
		var docs = this.__getDocs__(window.top.getBrowser().selectedBrowser.contentWindow);
		var images = [];
		for (var index in docs){
			var doc = docs[index];
	        var cssURI = CSSPrimitiveValue.CSS_URI;
	        var treeWalker = doc.createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, null, false);
	        var element, computedStyle, im, i;
	        var props = ['background-image', 'list-style-image', 'content', 'cursor'];
	        while ((element = treeWalker.nextNode()) !== null) {
	            computedStyle = element.ownerDocument.defaultView.getComputedStyle(element, null);
	            if (computedStyle) {
	                for (i = 0; i < props.length; i++) {
	                    im = computedStyle.getPropertyCSSValue(props[i]);
	                    if (im && typeof im.primitiveType != 'undefined' && im.primitiveType == cssURI) {
	                        images[im.getStringValue()] = 1;
	                    }
	                }
	            }
	        }
    	}
        return images;
	},
	
	/**
	* return all document objects of frames as array 
	* @param fr frame to find
	*/
	__getDocs__: function(fr) {
        docs = [];
		var frames, frames_length, i;
        if (fr) {
            frames = fr.frames;
            frames_length = frames.length;
            if (fr.document) {
                docs.push(fr.document);
            }
            for (i = 0; i < frames_length; i++) {
                docs=docs.concat(this.__getDocs__(frames[i]));
            }
        }
        return docs;
    },
    
    // Gets the size of a file, code from from web developer toolbar
	getFileSize : function(url) {
	    var cacheService = Components.classes["@mozilla.org/network/cache-service;1"].getService(Components.interfaces.nsICacheService);
	    var cacheSession = null;
	    var file = null;
	    var fileSize = null;
	    var readAccess   = Components.interfaces.nsICache.ACCESS_READ;
	    // Try to get the file size from the HTTP cache
	    try {
	        cacheSession = cacheService.createSession("HTTP", 0, true);
	        cacheSession.doomEntriesIfExpired = false;
	        file = cacheSession.openCacheEntry(url, readAccess, false);
	        // If there is a file
	        if(file) {
	            fileSize = file.dataSize;
	        }
	    } catch(exception) {
	        // Try to get the file size from the FTP cache
	        try {
	            cacheSession = cacheService.createSession("FTP", 0, true);
	            cacheSession.doomEntriesIfExpired = false;
	            file = cacheSession.openCacheEntry(url, readAccess, false);
	            if(file) {
	                fileSize = file.dataSize;
	            }
	        } catch(exception) {
	            fileSize = null;
	        }
	    }
	    // If the file size could not be retrieved from the cache,  try to download the file using ajax
	    if(!fileSize) {
	        try {
	            var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
	            fileSize = ioService.newChannelFromURI(ioService.newURI(url, null, null)).open().available();
	        } catch(exception) {
	            fileSize = null;
	        }
	    }
	    return fileSize;
	},
	
	obj2Str : function ( obj ){
		if(obj==null) return null;
		var cst = obj.constructor;
		switch(cst){
			case String: return '"'+obj+'"';
			case Number: return obj+"";
			case Date: return 'new Date(' + obj.getTime() + ')';
			case Array :
				var ar=[];
				for(var i=0;i<obj.length;i++) ar[i]=checkerUtil.obj2Str(obj[i]);
				return '['+ar.join(',')+']';
			case Object :
				var ar=[];
				for(var i in obj){
					ar.push('"'+(i+'')+'":'+checkerUtil.obj2Str(obj[i]));
				}
				return '{'+ar.join(',')+'}';
		}
		return null;
	},
	
	readFile : function(url) {
		try {
	        var request = new XMLHttpRequest();
	        request.open("get", url, false);
	        request.send(null);
	        source = request.responseText;
	    }  catch(exception)  {
	        source = null;
	    }
		return source;
	},
	
	getElementsByXPath : function(xpath){
		var result = content.document.evaluate(xpath, content.document, null, XPathResult.ANY_TYPE,null);
		var ret = [];
		var item;
		while(item = result.iterateNext()){
			ret.push(item);
		}
		return ret;
	},
	
	getSingleByXPath : function(xpath){
		var result = content.document.evaluate(xpath, content.document, null, XPathResult.ANY_TYPE,null);
		return result.iterateNext();
	}
	

};

